package org.cocopapaya.springws.ext.xml;

import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.xml.namespace.QNameUtils;
import org.springframework.xml.sax.SaxUtils;
import org.springframework.xml.validation.XmlValidator;
import org.springframework.xml.validation.XmlValidatorFactory;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class CustomXsdSchema implements XsdSchema, InitializingBean {

    private static DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    private static final String SCHEMA_NAMESPACE = "http://www.w3.org/2001/XMLSchema";

    private static final QName SCHEMA_NAME = QNameUtils.createQName(SCHEMA_NAMESPACE, "schema", "xsd");

    private Resource xsdResource;

    private Element schemaElement;

    static {
        documentBuilderFactory.setNamespaceAware(true);
    }

    /**
     * Create a new instance of the {@link SimpleXsdSchema} class.
     * <p/>
     * A subsequent call to the {@link #setXsd(Resource)} method is required.
     */
    public CustomXsdSchema() {
    }

    /**
     * Create a new instance of the  {@link SimpleXsdSchema} class with the specified resource.
     *
     * @param xsdResource the XSD resource; must not be <code>null</code>
     * @throws IllegalArgumentException if the supplied <code>xsdResource</code> is <code>null</code>
     */
    public CustomXsdSchema(Resource xsdResource) {
        Assert.notNull(xsdResource, "xsdResource must not be null");
        this.xsdResource = xsdResource;
    }

    /**
     * Set the XSD resource to be exposed by calls to this instances' {@link #getSource()} method.
     *
     * @param xsdResource the XSD resource
     */
    public void setXsd(Resource xsdResource) {
        this.xsdResource = xsdResource;
    }

    public String getTargetNamespace() {
        return schemaElement.getAttribute("targetNamespace");
    }

    public Source getSource() {
        return new DOMSource(schemaElement);
    }

    public XmlValidator createValidator() throws IOException {
        return CustomXmlValidatorFactory.createValidator(xsdResource, XmlValidatorFactory.SCHEMA_W3C_XML);
    }

    public void afterPropertiesSet() throws ParserConfigurationException, IOException, SAXException {
        Assert.notNull(xsdResource, "'xsd' is required");
        Assert.isTrue(this.xsdResource.exists(), "xsd '" + this.xsdResource + "' does not exist");
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        loadSchema(documentBuilder);
    }

    private void loadSchema(DocumentBuilder documentBuilder) throws SAXException, IOException {
        Document schemaDocument = documentBuilder.parse(SaxUtils.createInputSource(xsdResource));
        schemaElement = schemaDocument.getDocumentElement();
        Assert.isTrue(SCHEMA_NAME.getLocalPart().equals(schemaElement.getLocalName()),
                xsdResource + " has invalid root element : [" + schemaElement.getLocalName() + "] instead of [schema]");
        Assert.isTrue(SCHEMA_NAME.getNamespaceURI().equals(schemaElement.getNamespaceURI()), xsdResource +
                " has invalid root element: [" + schemaElement.getNamespaceURI() + "] instead of [" +
                SCHEMA_NAME.getNamespaceURI() + "]");
        Assert.hasText(getTargetNamespace(), xsdResource + " has no targetNamespace");
    }

    public String toString() {
        StringBuilder builder = new StringBuilder("SimpleXsdSchema");
        builder.append('{');
        builder.append(getTargetNamespace());
        builder.append('}');
        return builder.toString();
    }
}
